/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Service;

import DAO.DAOFactory;
import DAO.ProyectoDAO;
import DAO.TareaDAO;
import DAO.UsuarioDAO;
import DAO.exceptions.IllegalOrphanException;
import DAO.exceptions.NonexistentEntityException;
import DAO.exceptions.RollbackFailureException;
import Entidad.*;
import VO.TareaVO;
import VO.UsuarioVO;
import java.util.*;
import javax.persistence.EntityManager;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author xSebastiaNx
 */
public class TaskServices {
    
    public void createTask(TareaVO taskVO, EntityManager em)throws Exception{
        TareaDAO dao = (TareaDAO)DAOFactory.createDAO(DAOFactory.TAREA);
        int size = dao.getTareaCount(em);
        taskVO.setIdtarea(size+1);
        Tarea tarea = taskVO.toEntity();
        dao.create(tarea, em);
    }
    
    public TareaVO readTask(int idTask, EntityManager em){
        TareaDAO tareaDAO = (TareaDAO)DAOFactory.createDAO(DAOFactory.TAREA);
        Tarea entity = tareaDAO.findTarea(idTask, em);
        if(entity != null) {
           TareaVO vo = entity.toVO();
           return vo;
        }
        else {
            return null;
        }
    }
    
    public void dropTask(int idTask, EntityManager em) {
        TareaDAO tareadao = (TareaDAO)DAOFactory.createDAO(DAOFactory.TAREA);
        try {
            tareadao.destroy(idTask, em);
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        }
            
    }
    
    public String setMoments(int idTask, Date startDate, int duration, EntityManager em) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception{
        TareaDAO tareadao = (TareaDAO)DAOFactory.createDAO(DAOFactory.TAREA);
        Tarea entity = tareadao.findTarea(idTask, em);
        entity.setDuracion(duration);
        entity.setFecha(startDate);
        tareadao.edit(entity, em);
        if (tareadao.findTarea(idTask, em).getDuracion() == duration && tareadao.findTarea(idTask, em).getFecha().equals(startDate))
            return "Completado";
        else
            return "Datos no actualizados";
    }
    
    /**
     * Clase que retorna las Tareas de un usuario con un correo determinado.
     * @param correo
     * @param em
     * @return 
     */
    public List<TareaVO> getTasksByID(String correo, EntityManager em){
        UsuarioDAO dao = (UsuarioDAO)DAOFactory.createDAO(DAOFactory.USUARIO);
        List <TareaVO> listTasks = null;
        Usuario user = dao.findUsuario(correo, em);
        List<Tarea> myList = (List)user.getTareaCollection();
        Iterator it = myList.listIterator();
        while(it.hasNext()){
            Tarea tarea = (Tarea)it.next();
            listTasks.add(tarea.toVO());
        }
        return listTasks;
    }
    
    public List<TareaVO> getTasksByIDProyect(Integer id, EntityManager em){
        ProyectoDAO dao = (ProyectoDAO)DAOFactory.createDAO(DAOFactory.PROYECTO);
        List <TareaVO> listTasks = new ArrayList<TareaVO>();
        Proyecto proyecto = dao.findProyecto(id, em);
        List<Flujo> myFlowList = (List<Flujo>)proyecto.getFlujoCollection();
        for(Flujo flujo : myFlowList){
            List<Actividad> myActivityList = (List<Actividad>)flujo.getActividadCollection();
            for (Actividad actividad : myActivityList){
                List<Tarea> listTemporalTasks = (List<Tarea>)actividad.getTareaCollection();
                for (Tarea task : listTemporalTasks){
                    listTasks.add(task.toVO());
                }
            }
        }
        return listTasks;
    }

    public void updateTask(TareaVO t, EntityManager em) {
        try {
            TareaDAO dao = (TareaDAO)DAOFactory.createDAO(DAOFactory.TAREA);
            Tarea tarea = dao.findTarea(t.getIdtarea(), em);
            tarea.setPorcentaje(t.getPorcentaje());
            tarea.setDescripcion(t.getDescripcion());
            dao.edit(tarea, em);
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void assignColaborator(TareaVO tareaVo, UsuarioVO usuarioVo, EntityManager em) {
        try {
            TareaDAO dao = (TareaDAO) DAOFactory.createDAO(DAOFactory.TAREA);
            Tarea tarea = dao.findTarea(tareaVo.getIdtarea(), em);
            Usuario user = usuarioVo.toEntity();
            tarea.getUsuarioCollection().add(user);
            dao.edit(tarea, em);
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TaskServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
